
<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">广告位名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">更新时间：</label>
            <div class="layui-input-inline">
                <input type="text" id="utime" name="utime" placeholder="开始时间 到 结束时间" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>

    </div>
</form>

<div class="table-body">
	<table id="carouselSeat" lay-filter="carouselSeat"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;

        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;
        
        //执行渲染
        table.render({
            elem: '#carouselSeat', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'carouselSeat',
            url: "{:url('CarouselSeat/index')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'name', title: '广告位名称',align:'center',width:120},
                {field: 'code', title: '广告位模板编码',align:'center',width:160},
                {field: 'ctime',sort: true, title: '创建时间' ,align:'center',width:160},
                {field: 'utime',sort: true, title: '更新时间' ,align:'center',width:160},
                {field: 'sort', sort: true, title: '排序',align:'center',width:80},
                {width:160, title:'操作',align:'center', toolbar:'#carouselSeatBar'}
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });

        //search
        date.render({
            elem:'#utime',
            range: '到'
        });

        form.on('submit(search)', function(data){
            layui.table.reload('carouselSeat', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //layui弹出层
        $(document).on('click','.add',function(){
            JsGet("{:url('CarouselSeat/add')}", function(e){
                if ((e.hasOwnProperty('status') && !e.status) || (e.hasOwnProperty('code') && e.code == 0)) {
                    layer.msg(e.msg); return false;
                }

                window.box = layer.open({
                    type: 1,
                    content: e,
                    area: ['486px', '300px'],
                    title:'添加广告位'
                }); 
            });
            return false;
        });


        //ajax提交商品的添加
        form.on('submit(add)', function(data){
            $("#carousel_set_add_save").addClass("layui-btn-disabled");
            $('#carousel_set_add_save').attr("disabled", true);

            JsPost("{:url('CarouselSeat/add')}", data.field, function(res){
                if(res.status){
                    layer.close(window.box);
                    layer.msg(res.msg, {time:1300},function(){
                        table.reload('carouselSeat');
                    });
                }else{
                    $("#carousel_set_add_save").removeClass("layui-btn-disabled");
                    $('#carousel_set_add_save').removeAttr("disabled");

                    layer.msg(res.msg);
                }
            })
        });


        form.on('submit(edit)',function(data){
            $("#carousel_set_edit_save").addClass("layui-btn-disabled");
            $('#carousel_set_edit_save').attr("disabled", true);

            JsPost("{:url('CarouselSeat/edit')}", data.field, function(res){
                if(res.status){
                    layer.close(window.box);
                    layer.msg(res.msg,{time:1300},function(){
                        table.reload('carouselSeat');
                    });
                }else{
                    $("#carousel_set_edit_save").removeClass("layui-btn-disabled");
                    $('#carousel_set_edit_save').removeAttr("disabled");

                    layer.msg(res.msg);
                }
            })
        });


        //监听工具条
        table.on('tool(carouselSeat)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsGet("{:url('CarouselSeat/del')}?id=" + data.id, function(res){
                        //删除逻辑中判断只有一条数据时应该刷新前一页信息
                        layer.msg(res.msg);
                        if(res.status){
                            if(dataNum == 1){
                                currPage = currPage - 1;
                            }

                            table.reload('carouselSeat', {
                                page: {
                                    curr: currPage
                                }
                            });
                            return false;
                        }
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                JsGet("{:url('CarouselSeat/edit')}?id=" + data.id, function(e){
                    if ((e.hasOwnProperty('status') && !e.status) || (e.hasOwnProperty('code') && e.code == 0)) {
                        layer.msg(e.msg); return false;
                    }

                    window.box = layer.open({
                        type: 1,
                        content: e,
                        area: ['486px', '300px'],
                        title: '编辑广告位',
                    })
                });
            }
        });

    })
</script>

<script type="text/html" id="carouselSeatBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
